/**
 * 
 */
package net.microlinktech.testing.packet;

import java.text.DecimalFormat;
import java.util.Calendar;

import net.microlinktech.testing.bean.Meter;
import org.apache.commons.lang3.ArrayUtils;

import net.microlinktech.testing.codec.StringCoder;
import net.microlinktech.testing.codec.StringSubstring;

/**
 * @author zhangqing
 * 
 */
public class DICoder {

	private static final DecimalFormat voltFormat = new DecimalFormat("000.0");
	private static final DecimalFormat ampeFormat = new DecimalFormat("000.000");
	private static final DecimalFormat powerFormat = new DecimalFormat(
			"000000.00");

	private static final DecimalFormat dateFormat = new DecimalFormat("00");

	private Meter meter;
	private byte[] address;
	private byte[] dataIdents;
	private String message;

	public DICoder(Meter meter, byte[] address, byte[] dataIdents) {
		this.meter = meter;
		this.address = address;
		this.dataIdents = dataIdents;
	}

	//返回数据报文
	public byte[] reponsePacket() {
		StringSubstring stringSubString = new StringSubstring();
		byte[] cmd = new byte[4];
		for (int i = 0; i < cmd.length; i++) {
			cmd[i] = (byte) (dataIdents[i] - 0x33);
		}
		byte[] resDatas = null;
		switch (cmd[0]) {
		case 0x00:
			if (cmd[3] == 0x02) {// 数据采集
				if (cmd[2] == 0x01) {
					switch (cmd[1]) {
					case 1:
						message = "发送温度数据采集响应的帧为：    ";
						resDatas = getVoltage(meter.getTemperatureVal());
						break;
					case 2:
						message = "发送湿度数据采集响应的帧为：    ";
						resDatas = getVoltage(meter.getHumidityVal());
						break;
					case 3:
						message = "发送ph值数据采集响应的帧为：    ";
						resDatas = getVoltage(meter.getPhVal());
						break;
					case 4:
						message = "发送co2数据采集响应的帧为：    ";
						resDatas = getVoltage(meter.getCarbonVal());
						break;
					case 5:
						message = "发送土壤浓度数据采集响应的帧为：    ";
						resDatas = getVoltage(meter.getSoilDensity());
						break;
					}
				}
			}
			break;
		case 1: // 抄日期
			if (cmd[1] == 01 && cmd[2] == 00 && cmd[3] == 04) {
				message = "发送 获取当前日期 的帧为：    ";
				Calendar ca = Calendar.getInstance();
				String str = dateFormat.format(ca.get(Calendar.DAY_OF_MONTH))
						+ dateFormat.format(ca.get(Calendar.MONTH) + 1)
						+ dateFormat.format(ca.get(Calendar.YEAR) - 2000)
						+ dateFormat.format(ca.get(Calendar.DAY_OF_WEEK));
				byte[] datas = StringCoder.hexStringToByte(str);
				for (int i = 0; i < datas.length; i++) {
					datas[i] = (byte) (datas[i] + 0x33);
				}
				resDatas = datas;
			}
			break;
		case 2: // 抄时间
			if (cmd[1] == 01 && cmd[2] == 00 && cmd[3] == 04) {
				message = "发送 获取当前时间 的帧为：    ";
				Calendar ca = Calendar.getInstance();
				String str = dateFormat.format(ca.get(Calendar.SECOND))
						+ dateFormat.format(ca.get(Calendar.MINUTE))
						+ dateFormat.format(ca.get(Calendar.HOUR_OF_DAY));
				resDatas = StringCoder.hexStringToByte(str);
				byte[] datas = StringCoder.hexStringToByte(str);
				for (int i = 0; i < datas.length; i++) {
					datas[i] = (byte) (datas[i] + 0x33);
				}
				resDatas = datas;
			}
			break;
		case 3: // 组网命令响应
				if (cmd[1] == 01 && cmd[2] == 00 && cmd[3] == 04) {
					message = "发送 获取当前时间 的帧为：    ";
					Calendar ca = Calendar.getInstance();
					String str = dateFormat.format(ca.get(Calendar.SECOND))
							+ dateFormat.format(ca.get(Calendar.MINUTE))
							+ dateFormat.format(ca.get(Calendar.HOUR_OF_DAY));
					resDatas = StringCoder.hexStringToByte(str);
					byte[] datas = StringCoder.hexStringToByte(str);
					for (int i = 0; i < datas.length; i++) {
						datas[i] = (byte) (datas[i] + 0x33);
					}
					resDatas = datas;
				}
				break;
		case 4: // 读取网络状态
				if (cmd[1] == 01 && cmd[2] == 00 && cmd[3] == 04) {
					message = "发送 获取当前时间 的帧为：    ";
					Calendar ca = Calendar.getInstance();
					String str = dateFormat.format(ca.get(Calendar.SECOND))
							+ dateFormat.format(ca.get(Calendar.MINUTE))
							+ dateFormat.format(ca.get(Calendar.HOUR_OF_DAY));
					resDatas = StringCoder.hexStringToByte(str);
					byte[] datas = StringCoder.hexStringToByte(str);
					for (int i = 0; i < datas.length; i++) {
						datas[i] = (byte) (datas[i] + 0x33);
					}
					resDatas = datas;
				}
				break;
		case 5: // 读取版本号
				if (cmd[1] == 01 && cmd[2] == 00 && cmd[3] == 04) {
					message = "发送 获取当前时间 的帧为：    ";
					Calendar ca = Calendar.getInstance();
					String str = dateFormat.format(ca.get(Calendar.SECOND))
							+ dateFormat.format(ca.get(Calendar.MINUTE))
							+ dateFormat.format(ca.get(Calendar.HOUR_OF_DAY));
					resDatas = StringCoder.hexStringToByte(str);
					byte[] datas = StringCoder.hexStringToByte(str);
					for (int i = 0; i < datas.length; i++) {
						datas[i] = (byte) (datas[i] + 0x33);
					}
					resDatas = datas;
				}
				break;
		default:
			break;
		}
		return AutoFrame(resDatas);
	}

	/**
	 * 获取电压的值
	 * 
	 * @param voltage
	 * @return
	 */
	private byte[] getVoltage(float voltage) {
		String fvalue = voltFormat.format(voltage).replace(".", "");
		byte[] dataTmp = StringCoder.hexStringToByte(fvalue);
		ArrayUtils.reverse(dataTmp);
		for (int i = 0; i < dataTmp.length; i++) {
			dataTmp[i] = (byte) (dataTmp[i] + 0x33);
		}
		return dataTmp;
	}

	/**
	 * 获取电流值
	 * 
	 * @param ampere
	 * @return
	 */
	private byte[] getAmpere(float ampere) {
		String fampere = ampeFormat.format(ampere).replace(".", "");
		byte[] dataTmp = StringCoder.hexStringToByte(fampere);
		ArrayUtils.reverse(dataTmp);
		for (int i = 0; i < dataTmp.length; i++) {
			dataTmp[i] = (byte) (dataTmp[i] + 0x33);
		}
		return dataTmp;
	}

	/**
	 * 获取电量
	 * 
	 * @param power
	 * @return
	 */
	private byte[] getPower(double power) {
		String fpower = powerFormat.format(power).replace(".", "");
		byte[] dataTmp = StringCoder.hexStringToByte(fpower);
		ArrayUtils.reverse(dataTmp);
		for (int i = 0; i < dataTmp.length; i++) {
			dataTmp[i] = (byte) (dataTmp[i] + 0x33);
		}
		return dataTmp;
	}

	/**
	 * 根据645协议组包发送
	 * 
	 * @param data
	 * @return
	 */
	public byte[] AutoFrame(byte[] data) {
		byte[] frame = null;
		if (data != null) {
			frame = new byte[data.length + 1 + 6 + 1 + 1 + 1 + 4 + 1 + 1];
			frame[0] = 0x68;
			System.arraycopy(address, 0, frame, 1, 6); // 地址
			frame[7] = 0x68;
			frame[8] = (byte) 0x91;
			byte length = (byte) (4 + data.length);
			frame[9] = length;
			System.arraycopy(dataIdents, 0, frame, 10, 4);
			System.arraycopy(data, 0, frame, 14, data.length);
			frame[14 + data.length] = checkSum8(frame, data.length + 1 + 6 + 1
					+ 1 + 1 + 4);
			frame[14 + data.length + 1] = 0x16;
		} else {
			frame = new byte[1 + 6 + 1 + 1 + 1 + 4 + 1 + 1];
			frame[0] = 0x68;
			System.arraycopy(address, 0, frame, 1, 6); // 地址
			frame[7] = 0x68;
			frame[8] = (byte) 0x91;
			frame[9] = 4;
			System.arraycopy(dataIdents, 0, frame, 10, 4);
			frame[14] = checkSum8(frame, 1 + 6 + 1 + 1 + 1 + 4);
			frame[15] = 0x16;
		}
		return frame;
	}

	/**
	 * 根据376.2协议组包发送
	 *
	 * @param data
	 * @return
	 */
	public byte[] AutoFrame376_2(byte[] data) {
		byte[] frame = null;
		if (data != null) {
			frame = new byte[data.length + 1 + 6 + 1 + 1 + 1 + 4 + 1 + 1];
			frame[0] = 0x68;
			System.arraycopy(address, 0, frame, 1, 6); // 地址
			frame[7] = 0x68;
			frame[8] = (byte) 0x91;
			byte length = (byte) (4 + data.length);
			frame[9] = length;
			System.arraycopy(dataIdents, 0, frame, 10, 4);
			System.arraycopy(data, 0, frame, 14, data.length);
			frame[14 + data.length] = checkSum8(frame, data.length + 1 + 6 + 1
					+ 1 + 1 + 4);
			frame[14 + data.length + 1] = 0x16;
		} else {
			frame = new byte[1 + 6 + 1 + 1 + 1 + 4 + 1 + 1];
			frame[0] = 0x68;
			System.arraycopy(address, 0, frame, 1, 6); // 地址
			frame[7] = 0x68;
			frame[8] = (byte) 0x91;
			frame[9] = 4;
			System.arraycopy(dataIdents, 0, frame, 10, 4);
			frame[14] = checkSum8(frame, 1 + 6 + 1 + 1 + 1 + 4);
			frame[15] = 0x16;
		}
		return frame;
	}





	/**
	 * CS校验
	 * 
	 * @param buf
	 * @param len
	 * @return
	 */
	public byte checkSum8(byte[] buf, int len) {
		byte sum = 0;
		while ((len--) > 0) {
			sum += buf[len];
		}
		return sum;
	}

	public String getMessage() {
		return message;
	}

}
